package com.lixw.langchain.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
 * @ClassName: WeatherService
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/25 09:59
 **/
@Service
public class WeatherService {
    public static final String API_KEY = System.getenv("WEATHER_KEY")==null?"2d14b28e564948a7843b276117a3ef21":System.getenv("WEATHER_KEY");

    public static final String BASE_URL = "https://qd6yvxtrp9.re.qweatherapi.com/v7/weather/now?location=%s&key=%s";

    public JsonNode getWeatherV7(String city) throws JsonProcessingException {
        String url = String.format(BASE_URL, city, API_KEY);

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        String response = new RestTemplate(factory).getForObject(url, String.class);

        return new ObjectMapper().readTree(response);
    }
}